<?php

ini_set('display_errors', 1);
error_reporting(-1);
require_once '/data/wwwroot/g_vendor/autoload.php';

use Biz\model\GoodsBasicInfo;
use Biz\model\GoodsExtendInfo;
use Biz\model\GoodsSku;
use Biz\model\GoodsSkuAttr;

switch ($argv[1]) {
    case 'goods':
        loadGoodsCsv($argv[2]);
        break;
    case 'shop':
        loadShopCsv($argv[2]);
        break;
    default:
        echo "请输入正确的参数";
}

function loadGoodsCsv($fileName)
{
    $handle = fopen($fileName, 'r');
    while (($data = fgetcsv($handle)) !== FALSE) {
        $data = array_filter(array_map('trim', $data));
        if (count($data) == 4) {
            copyGoods($data[0], $data[2], $data[3]);
        }
    }
}

function loadShopCsv($fileName)
{
    $handle = fopen($fileName, 'r');
    while (($data = fgetcsv($handle)) !== FALSE) {
        $data = array_filter(array_map('trim', $data));
        if (count($data) == 3) {
            copyShop($data[0], $data[1], $data[2]);
        }
    }
}


function copyShop($sourceShopId, $targetShopId, $targetTemplId)
{
    $goodsList = GoodsBasicInfo::select('goods_id', [
        'shop_id' => $sourceShopId,
        'status' => GoodsBasicInfo::STATUS_APP_CAN_SEE_MAP,
    ]);
    foreach ($goodsList as $goodsId) {
        copyGoods($goodsId, $targetShopId, $targetTemplId);
    }
    echo "复制店铺:{$sourceShopId} 的商品" . count($goodsList) . "个至店铺:{$targetShopId}成功" . PHP_EOL;
}


function copyGoods($sourceGoodsId, $targetShopId, $targetTemplId)
{
    $goodsBaseInfo = GoodsBasicInfo::get("*", ['goods_id' => $sourceGoodsId]);
    if (!$goodsBaseInfo) {
        return false;
    }
    unset($goodsBaseInfo['goods_id']);
    $goodsBaseInfo = array_merge($goodsBaseInfo, [
        'uid' => $targetShopId,
        'shop_id' => $targetShopId,
        'shipping_templ_id' => $targetTemplId,
        'status' => GoodsBasicInfo::STATUS_ONLINE
    ]);
    GoodsBasicInfo::insert($goodsBaseInfo);
    $targetGoodsId = GoodsBasicInfo::id();
    $GoodsExtendInfo = GoodsExtendInfo::get("*", ['goods_id' => $sourceGoodsId,]);
    $GoodsExtendInfo = array_merge($GoodsExtendInfo, [
        'goods_id' => $targetGoodsId,
        'uid' => $targetShopId,
        'shop_id' => $targetShopId,
        'shipping_templ_id' => $targetTemplId
    ]);
    GoodsExtendInfo::insert($GoodsExtendInfo);
    $skuAttrs = GoodsSkuAttr::select("*", [
        'goods_id' => $sourceGoodsId,
        'status' => GoodsSkuAttr::STATUS_PASSED
    ]);
    $skuAttrMap = [];
    foreach ($skuAttrs as $skuAttr) {
        $skuAttrId = $skuAttr['id'];
        unset($skuAttr['id']);
        $skuAttr = array_merge($skuAttr, [
            'goods_id' => $targetGoodsId,
            'shop_id' => $targetShopId
        ]);
        GoodsSkuAttr::insert($skuAttr);
        $skuAttrMap[$skuAttrId] = GoodsSkuAttr::id();
    }
    $skus = GoodsSku::select("*", [
        'goods_id' => $sourceGoodsId,
        'status' => GoodsSku::STATUS_PASSED
    ]);
    foreach ($skus as $sku) {
        unset($sku['id']);
        $sku = array_merge($sku, [
            'goods_id' => $targetGoodsId,
            'shop_id' => $targetShopId,
            'sku_attr_t1' => $sku['sku_attr_t1'] ? $skuAttrMap[$sku['sku_attr_t1']] : $sku['sku_attr_t1'],
            'sku_attr_t2' => $sku['sku_attr_t2'] ? $skuAttrMap[$sku['sku_attr_t2']] : $sku['sku_attr_t2'],
        ]);
        GoodsSku::insert($sku);
    }
    echo "复制商品:{$sourceGoodsId} 至店铺:{$targetShopId}成功,新商品ID:{$targetGoodsId}" . PHP_EOL;
}
